- R语言的特征
- R工作界面介绍
- R的基础设定
- R的基础操作
- R数据分析的例子
- R的学习方式与资料
2017年10月10日
install.packages("tidyverse")library(tidyverse)改变默认语言(建议设定为英文)
etc\Rconsole文件,language = en.Renviron,LANGUAGE=en查询当前工作目录getwd() 改变默认工作目录 setwd("D:/work")
注意Mac与Windows电脑路径表达差异
Chester Ismay
通过菜单file->reopen with encoding,简体中文通常可尝试选择gb2312解决
自动生成的模板文件已经设定了头部yaml
简单的文本和代码以及相关操作说明。
通过cmd+op+I插入代码。
表示引用
编辑完成之后可以通过Rstudio上的Knit按钮进行转码打印
呈现全球国家的预期寿命(life expectancy)和人均GDP(GDP per capita)之间的关系. Hans Rosling曾经做个一个TED演讲。
下面将使用 dplyr包 (用于数据处理 data wrangling) 和 ggplot2 (用于作图,visualization) .
首先要确保这些包都安装了(installed).
在markdown中加载(Load)这些包:
library(dplyr) library(ggplot2)
# gapminder <- read.csv("https://stat.duke.edu/~mc301/data/gapminder.csv")
# setwd("/Users/liding/E/Bdata/liding17/2017R/lesson3/")
gapminder <- read.csv("gapminder.csv")
以gapminder 数据集(dataset)开始
选择年份(year)变量等于2007的案例
将筛选出来的案例存到一个新的数据集gap07
gap07 <- gapminder %>% filter(year == 2007)
任务: 呈现 gdpPercap 与lifeExp之间的关系.
qplot(x = gdpPercap, y = lifeExp, data = gap07)
任务: 各个大陆的点使用不同的颜色.
qplot(x = gdpPercap, y = lifeExp, color = continent, data = gap07)
任务: 在设定点的大小与人口规模成正比.
qplot(x = gdpPercap, y = lifeExp, color = continent, size = pop,data = gap07)
#library(dplyr)
library(gganimate)
library(ggplot2)
library(readr)
# gapminder <- read_tsv("gapminderDataFiveYear.tsv")
gapminder <- read.csv("https://stat.duke.edu/~mc301/data/gapminder.csv")
gapminder_plot <- ggplot(gapminder) +
aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop,
frame = year) +
geom_point(alpha = 0.4) +
scale_x_log10()
# gapminder_plot
gganimate(gapminder_plot, convert='gm convert', filename = "gapminder-gganimate.gif")
这个例子来自 https://paulvanderlaken.com/2017/06/20/tidyverse-example-trump-approval-rate/
if (!require(hrbrthemes)) install.packages('hrbrthemes')
if (!require(rvest)) install.packages('rvest')
library(tidyverse)
对两个网页做同样的事情
list(
Obama="http://m.rasmussenreports.com/public_content/politics/obama_administration/obama_approval_index_history",
Trump="http://m.rasmussenreports.com/public_content/politics/trump_administration/trump_approval_index_history"
) %>%
map_df(~{
read_html(.x) %>%
html_table() %>%
.[[1]] %>%
tbl_df() %>%
select(date=Date, approve=`Total Approve`, disapprove=`Total Disapprove`)
}, .id="who") -> ratings
data <- mutate_at(ratings,
c("approve", "disapprove"),
function(x) as.numeric(gsub("%", "", x,fixed=TRUE))/100) %>%
mutate(date = lubridate::dmy(date)) %>%
filter(!is.na(approve)) %>%
group_by(who) %>%
arrange(date) %>%
mutate(dnum = 1:n()) %>%
ungroup()
## Warning in (function (x) : NAs introduced by coercion
## Warning in (function (x) : NAs introduced by coercion
ggplot(data,aes(dnum, approve, color=who)) +
geom_hline(yintercept = 0.5, size=0.5) +
geom_point(size=0.25) +
scale_y_percent(limits=c(0,1)) +
scale_color_manual(name=NULL, values=c("Obama"="#313695", "Trump"="#a50026")) +
labs(x="Day in office", y="Approval Rating",
title="Presidential approval ratings from day 1 in office",
subtitle="Data was taken solely from Trump's favorite polling site (Ramussen)",
caption="Data Source: \nCode: rasmussenreports.com")
help.start() 打开帮助文档首页help("foo")或?foo 查看函数foo的帮助(引号可以省略)help.search("foo")或??foo 以foo为关键词搜索本地帮助文档example("foo") 函数foo的使用示例(引号可以省略)RSiteSearch("foo") 以foo为关键词搜索在线文档和邮件列表存档apropos("foo", mode="function") 列出名称中含有foo的所有可用函data() 列出当前已加载包中所含的所有可用示例数据集vignette() 列出当前已安装包中所有可用的vignette文档vignette("foo")为主题foo显示指定的vignette文档管道操作符 %>% 是Stefan Milton Bache and Hadley Wickham在magrittr中首创的.
将上一段命令的结果作为下一行命令的首个默认输入元素,从而可以:
其他资料
tidydata简介: http://www.jstatsoft.org/v59/i10/paper
tidyverse的学习网址:https://www.tidyverse.org/learn/
Rmarkdown撰写的书籍:https://bookdown.org/home/
To be continued